<?php
// +----------------------------------------------------------------------
// | CMS前端公共变量区
// +----------------------------------------------------------------------
// | Copyright (c) 2023
// +----------------------------------------------------------------------
$zhlcms = 'Web_BaseServe';
$mid = isset($mid) ? $mid : 0;
$eid = isset($eid) ? $eid : 0;
$page = isset($page) ? $page : 1;
$tmplUrl = isset($tmplUrl) ? $tmplUrl : '';
$menuCode = $tmplUrl;
$menuInfo = array();
$essayInfo = array();
$mids = array();
$menuFidList = array();
$seo_title = '';
$seo_key = '';
$seo_desc = '';

if($mid){
    $menuInfo = $zhlcms::menuInfo($mid);
}else{
    $menuInfo = $zhlcms::menuInfos(" and (tmpl='$tmplUrl' or tmplinfo='$tmplUrl') ");
}

if($eid){
    $menuCode = !$menuInfo ? $tmplUrl : $menuInfo['mould_code'];
    // 判断表是否存在
    $db = Model::conn('mysql');
    $dbTable = $db->db_fix.$menuCode;
    $sql = "select * from information_schema.tables where table_schema='$db->db_name' and table_name = '$dbTable'";
    $tableExist = Model::conn('mysql')->query($sql, 1);
    if($tableExist){
        $essayInfo = $zhlcms::essayInfos($menuCode, $eid);
        if($essayInfo){
            $eid = $essayInfo['id'];
            $menuInfo = $zhlcms::menuInfos(array('id'=>$essayInfo['menu_id']));
        }
    }
}
// 导航存在
if($menuInfo){
    $mid = $menuInfo['id'];
    // 栏目seo
    $seo_title = $menuInfo['seotitle'] ? $menuInfo['seotitle'] : $menuInfo['menuname'];
    $seo_key = $menuInfo['seokey'] ? $menuInfo['seokey'] : $menuInfo['menuname'];
    $seo_desc = $menuInfo['seodesc'] ? $menuInfo['seodesc'] : $menuInfo['menuname'];
    // 查找所有父级
    $sql = "select t2.* from (
                  select @r as _id, 
                    (select @r := fid from zhl_sysmenu where id = _id) as fid, 
                    @l := @l + 1 as lvl 
                  from (select @r := ".$menuInfo['id'].", @l := 0) vars, zhl_sysmenu h 
                  where @r <> 0 and fid > 0
            ) t1 
            join zhl_sysmenu t2 on t1._id = t2.id 
            order by t1.lvl desc";
    $menuFidList = Model::conn('mysql')->query($sql, 1);
    $mids = $menuFidList ? array_column($menuFidList, 'id') : array();
}

// 文章seo
if($essayInfo){
    $seo_title = $essayInfo['title'];
    $seo_key = $essayInfo['title'];
    if(isset($essayInfo['content'])){
        $seo_desc = $essayInfo['content'] ? mbSubstr(strip_tags(strHtml($essayInfo['content'])), 0, 150, '…') : '';
    }
    $seo_desc = $seo_desc ? $seo_desc : $essayInfo['title'];
}

$sys_js_count = '<script type="text/javascript">';
$sys_js_count .= 'var http_url = "'.SYS_HOST.'"; ';
$sys_js_count .= 'var http_link = "'.SYS_LINK.'"; ';
$sys_js_count .= 'var http_fix = "'.SYS_URL_FIX.'"; ';
$sys_js_count .= 'var http_title = "'.$seo_title.'"; ';
$sys_js_count .= '</script>'.PHP_EOL;
if($GLOBALS['ADMIN']['counts_flag'] == 1){
    $sys_js_count .= '<script type="text/javascript" src="'.SYS_URL_PATH('c_client_info').'"></script>'.PHP_EOL;
    $sys_js_count .= '<script type="text/javascript" src="'.SYS_LINK.'plug/counts/zhlcount3.js?f=open&v='.V_CSSJS.'"></script>'.PHP_EOL;
}